import { Column, Index, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn } from "typeorm";
import { EntityModel } from "@midwayjs/orm";
import { RescueInfo } from "./RescueInfo.entity";
import { RescueRecord } from "./RescueRecord.entity";
import { MaterialDonateRecord } from "./MaterialDonateRecord.entity";
import { Child } from "./Child.entity";
import { UserChild } from "./UserChild.entity";

@Index("phone", ["phone"], { unique: true })
@Index("email", ["email"], { unique: true })
@EntityModel("users", { schema: "heart_neighbor" })
export class Users {
  @PrimaryGeneratedColumn({
    type: "bigint",
    name: "id",
    comment: "用户ID",
    unsigned: true,
  })
  id!: string;

  @Column("varchar", {
    name: "phone",
    unique: true,
    comment: "电话",
    length: 20,
  })
  phone!: string;

  @Column("varchar", {
    name: "email",
    nullable: true,
    unique: true,
    comment: "邮箱",
    length: 100,
  })
  email!: string | null;

  @Column("date", { name: "birthday", nullable: true, comment: "生日" })
  birthday!: string | null;

  @Column("varchar", { name: "password", comment: "密码", length: 300 })
  password!: string;

  @Column("varchar", {
    name: "nickname",
    nullable: true,
    comment: "昵称",
    length: 100,
  })
  nickname!: string | null;

  @Column("tinyint", {
    name: "role",
    comment: "角色，1=志愿者，2等于监护人",
    width: 1,
    default: () => "'1'",
  })
  role!: boolean;

  @Column("varchar", {
    name: "user_sign",
    nullable: true,
    comment: "签名",
    length: 100,
  })
  userSign!: string | null;

  @Column("bigint", { name: "child_id", nullable: true, comment: "关联儿童ID" })
  childId!: string | null;    

  @Column("bigint", {
    name: "certificate_id",
    nullable: true,
    comment: "关联证书ID",
  })
  certificateId!: string | null;

  @Column("varchar", {
    name: "certificate_types",
    nullable: true,
    comment: "证书类型，1=救助证书，2=捐赠证书，逗号分隔",
    length: 255,
  })
  certificateTypes!: string | null;

  @Column("tinyint", {
    name: "is_disabled",
    comment: "是否禁用，0=启用，1=禁用",
    width: 1,
    default: () => "'0'",
  })
  isDisabled!: boolean;

  @Column("timestamp", {
    name: "created_at",
    comment: "创建时间",
    default: () => "CURRENT_TIMESTAMP",
  })
  createdAt!: Date; 

  @Column("varchar", {
    name: "avatar",
    nullable: true,
    comment: "头像",
    length: 255,
  })
  avatar!: string | null;

  // 一对多关系：一个用户可以发起多个救助信息
  @OneToMany(() => RescueInfo, (rescueInfo) => rescueInfo.guardian, { createForeignKeyConstraints: false })
  rescueInfos!: RescueInfo[];

  // 一对多关系：一个用户可以有多个救助记录
  @OneToMany(() => RescueRecord, (record) => record.user, { createForeignKeyConstraints: false })
  rescueRecords!: RescueRecord[];

  // 一对多关系：一个用户可以有多个物资捐赠记录
  @OneToMany(() => MaterialDonateRecord, (record) => record.user, { createForeignKeyConstraints: false })
  materialDonateRecords!: MaterialDonateRecord[];

  // 多对一关系：多个用户可以关联一个儿童（监护人关系）- 保留原有字段兼容性
  @ManyToOne(() => Child, (child) => child.users, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "child_id" })
  child!: Child;

  // 一对多关系：一个用户可以有多个儿童关联记录
  @OneToMany(() => UserChild, (userChild) => userChild.user, { createForeignKeyConstraints: false })
  userChildren!: UserChild[];
}
